package com.sunsun_china.www.aq;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.sunsun_china.www.aq.base.MyApplication;

@SuppressLint("NewApi") public class EditDeviceActivity extends ActionBarActivity {

	MyApplication mApp;
	TextView mTextViewDid;
	TextView mTextViewName;
	EditText mEditTextName;
	Button mButtonDelete;
	Context mContext;
	EditDeviceActivity mThis;
	EditText mEditTextPassword;
	CheckBox mCheckBoxPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_device);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);
		mApp = (MyApplication)getApplication();
		mApp.mEditDeviceUi = this;
		if (mApp.mEditDeviceInfo == null) {
			this.finish();
		}
		mContext = this;
		mThis = this;
		mTextViewName = (TextView) findViewById(R.id.edit_device_textView1);
		mTextViewDid = (TextView) findViewById(R.id.edit_device_textView2);
		mEditTextName = (EditText) findViewById(R.id.edit_device_editText1);
		mButtonDelete = (Button) findViewById(R.id.edit_device_button1);
		mEditTextPassword = (EditText) findViewById(R.id.edit_device_editText2);
		mCheckBoxPassword = (CheckBox) findViewById(R.id.edit_device_checkBox1);

		mEditTextName.setText(mApp.mEditDeviceInfo.mDeviceName);
		mTextViewName.setText(mApp.mEditDeviceInfo.mDeviceName);
		mTextViewDid.setText(mApp.mEditDeviceInfo.mDid);

		mEditTextPassword.setKeyListener(null);
		mEditTextPassword.setText("********");

		mButtonDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(mContext)
						.setTitle("删除确认")
						.setMessage("是否删除设备？")
						.setPositiveButton("否", null)
						.setNegativeButton("是", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// 删除设备
								mApp.removeDevice(mApp.mEditDeviceInfo);
								mThis.finish();
							}
						})
						.show();
			}
		});

		mCheckBoxPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// 密码显示状态改变
				if (isChecked) {
					mEditTextPassword.setText(mApp.mEditDeviceInfo.mPassword);
				} else {
					mEditTextPassword.setText("********");
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_edit_device, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			// 返回
			mApp.mEditDeviceUi = null;
			this.finish();
			return true;
		}
		if (id == R.id.edit_device_action_settings1) {
			// 保存并返回
			mApp.mEditDeviceInfo.mDeviceName = mEditTextName.getText().toString();
			mApp.addDevice(mApp.mEditDeviceInfo);
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果是返回键
		if(keyCode == KeyEvent.KEYCODE_BACK){
			mApp.mEditDeviceUi = null;
		}
		return super.onKeyDown(keyCode, event);
	}
}
